package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.*;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @description:套餐管理
 * @author: yanhongwei
 * @date: 2023/5/24 20:21
 * @param:
 * @return:
 **/

@RestController
@RequestMapping("/setmeal")
@Slf4j
@Api("套餐相关接口")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private CategoryService categoryService;

    /**
     * @description:保存套餐信息
     * @author: yanhongwei
     * @date: 2023/5/25 15:34
     * @param: [setmealDto]
     * @return: com.itheima.reggie.common.R<java.lang.String>
     **/
    @PostMapping
    @ApiOperation(value = "新增套餐接口")
    public R<String> save(@RequestBody SetmealDto setmealDto) {
        log.info("setmealDto:{}" + setmealDto);

        setmealService.saveWithDish(setmealDto);

        return R.success("添加套餐信息成功");
    }

    /**
     * @description:查询套餐分页信息
     * @author: yanhongwei
     * @date: 2023/5/25 17:10
     * @param: [page, pageSize, name]
     * @return: com.itheima.reggie.common.R<com.baomidou.mybatisplus.extension.plugins.pagination.Page>
     **/
    @GetMapping("/page")
    @ApiOperation(value = "套餐分页查询接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "页码",required = true),
            @ApiImplicitParam(name = "pageSize",value = "每页记录数",required = true),
            @ApiImplicitParam(name = "name",value = "套餐名称",required = false)
    })
    public R<Page> page(int page, int pageSize, String name) {

        //定义分页对象
        Page<Setmeal> page1 = new Page(page, pageSize);

        Page<SetmealDto> page2 = new Page<>();

        //添加查询条件,根据name进行like模糊查询
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.like(name != null, Setmeal::getName, name);
        //添加排序条件，根据更新条件时间降序排列
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);

        //查询分页信息
        setmealService.page(page1, queryWrapper);

        //拷贝信息
        BeanUtils.copyProperties(page1, page2, "records");

        List<Setmeal> records = page1.getRecords();

        //将分类名称传到SetmealDto中
        List<SetmealDto> setmealDtoList = records.stream().map((item) -> {
            SetmealDto setmealDto = new SetmealDto();

            BeanUtils.copyProperties(item, setmealDto);

            Category category = categoryService.getById(item.getCategoryId());

            //判断是否为空,获取分类名称
            if (category != null) {
                String categoryName = category.getName();
                setmealDto.setCategoryName(categoryName);
            }

            return setmealDto;


        }).collect(Collectors.toList());

        //分页对象setmealDtoList获取数据
        page2.setRecords(setmealDtoList);

        return R.success(page2);
    }

    /**
     * @description:删除套餐并且删除与套餐关联的菜品信息
     * @author: yanhongwei
     * @date: 2023/5/26 10:32
     * @param: [ids]
     * @return: com.itheima.reggie.common.R<java.lang.String>
     **/
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids){
        log.info("ids:{}",ids);

        setmealService.removeWithDIsh(ids);

        return R.success("套餐删除成功");
    }

    /**
     * @description:根据条件查询对应的套餐数据
     * @author: yanhongwei
     * @date: 2023/5/25 14:35
     * @param: [dish]
     * @return: com.itheima.reggie.common.R<java.util.List<com.itheima.reggie.entity.Dish>>
     **/
    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal){

        //构造查询条件
        LambdaQueryWrapper<Setmeal> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId()!=null,Setmeal::getCategoryId,setmeal.getCategoryId());
        //添加条件，查询状态为1（起售状态）的菜品
        queryWrapper.eq(setmeal.getStatus()!=null,Setmeal::getStatus,setmeal.getStatus());
        //添加排序条件
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);

        List<Setmeal> list = setmealService.list(queryWrapper);

        return R.success(list);
    }


    /**
     * @description:根据条件查询对应的套餐中的菜品数据
     * @author: yanhongwei
     * @date: 2023/5/25 14:35
     * @param: [dish]
     * @return: com.itheima.reggie.common.R<java.util.List<com.itheima.reggie.entity.Dish>>
     **/
    @GetMapping("dish/{id}")
    public R<List<SetmealDish>> list(@PathVariable("id") Long id){

        //构造查询条件
        LambdaQueryWrapper<SetmealDish> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(id!=null,SetmealDish::getSetmealId,id);
        //添加排序条件
        queryWrapper.orderByAsc(SetmealDish::getSort).orderByDesc(SetmealDish::getUpdateTime);

        List<SetmealDish> list = setmealDishService.list(queryWrapper);


        return R.success(list);
    }


}
